
//
//  PickerViewTableViewCell.m
//  MiSterLinShoes
//
//  Created by Fidetro on 2016/11/11.
//  Copyright © 2016年 Dtston. All rights reserved.
//

#import "PickerViewTableCell.h"
@interface PickerViewTableCell ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    UIToolbar *_inputAccessoryView;
    UIPickerView *_inputView;
}

@property(nonatomic,strong,readwrite)NSArray *selectArray;
@property (nonatomic,strong) UIPickerView *pickerView;

@end

@implementation PickerViewTableCell

- (void)setDATAArray:(NSArray *)dataArray{
    
    _DATAArray = dataArray;
    [self.pickerView reloadAllComponents];
    
}

- (void)setDetailArray:(NSArray *)detailArray{
    
    _detailArray = detailArray;
    [self.pickerView reloadAllComponents];
    
}
-(BOOL)canBecomeFirstResponder{
    
    return YES;
    
}

-(instancetype)initWithDataSource:(NSArray *)dataArray detailSource:(NSArray *)detailArray{
    
    if ([super initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:PICKERCELL]) {
        self.DATAArray = dataArray;
        self.detailArray = detailArray;
        
    }
    
    return self;
    
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    if ([super initWithCoder:aDecoder]) {
        
    }
    
    return self;
}

- (UIView *)inputAccessoryView{
    
    if (!_inputAccessoryView) {
        
        
        UIView *toolBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
        
        toolBar.backgroundColor = [UIColor colorWithHexString:@"f8f7f7"];
        UIView *lineView = [[UIView alloc]init];
        UIButton *cancelButton = [[UIButton alloc]init];
        UIButton *confirmButton = [[UIButton alloc]init];
        [toolBar addSubview:lineView];
        [toolBar addSubview:cancelButton];
        [toolBar addSubview:confirmButton];
        lineView.backgroundColor = [UIColor colorWithHexString:@"f8f7f7"];
        [lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.mas_equalTo(toolBar.mas_centerX);
            make.centerY.mas_equalTo(toolBar.mas_centerY);
            make.width.mas_equalTo(1);
            make.top.mas_equalTo(toolBar.mas_top).mas_offset(10);
            make.bottom.mas_equalTo(toolBar.mas_bottom).mas_offset(-10);
            
        }];
        
        
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [cancelButton setBackgroundColor:[UIColor colorWithHexString:@"f8f7f7"]];
        [cancelButton.titleLabel setFont:[UIFont systemFontOfSize:13.f]];
        
        [cancelButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.and.bottom.mas_equalTo(toolBar);
            make.left.mas_equalTo(toolBar.mas_left).mas_offset(15);
            make.right.mas_equalTo(lineView.mas_left);
            
        }];
        [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
        [confirmButton.titleLabel setFont:[UIFont systemFontOfSize:13.f]];
        confirmButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;

        [confirmButton setTitleColor:[UIColor colorWithHexString:@"dab866"] forState:UIControlStateNormal];
        [confirmButton setBackgroundColor:[UIColor colorWithHexString:@"f8f7f7"]];
        
        [confirmButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.and.bottom.mas_equalTo(toolBar);
            make.right.mas_equalTo(toolBar.mas_right).mas_offset(-15);
            make.left.mas_equalTo(lineView.mas_right);
            
        }];
        [cancelButton addTarget:self action:@selector(resignFirstResponder) forControlEvents:UIControlEventTouchUpInside];
        [confirmButton addTarget:self action:@selector(selectDataSource:) forControlEvents:UIControlEventTouchUpInside];
        
//        UIToolbar * toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
//        
//        UIBarButtonItem * DoneItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(resignFirstResponder)];
//        UIBarButtonItem * enterItem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(selectDataSource:)];
//        UIBarButtonItem *springItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
//        toolBar.items = @[DoneItem,springItem,enterItem];
        
        return toolBar;
        
    }
    
    return _inputAccessoryView;
    
}
- (void)selectDataSource:(UIBarButtonItem *)sender{
    
    [self resignFirstResponder];
    NSMutableArray *selectArray = [NSMutableArray array];
    for (NSInteger i = 0; i < self.DATAArray.count; i++) {
        [selectArray addObject:self.DATAArray[i][[self.pickerView selectedRowInComponent:i]]];
    }
    NSMutableArray *rowInComponentArray = [NSMutableArray array];
    for (NSInteger i = 0; i < self.DATAArray.count; i++) {
        [rowInComponentArray addObject:[NSString stringWithFormat:@"%ld",i]];
    }
    
    if (self.select_block) {
        self.select_block(selectArray);
    }
    [self.delegate selectPickerDataSource:selectArray selectedRowInComponent:rowInComponentArray];
    
}
-(UIView *)inputView{
    
    if (!_inputView) {
        
        self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 200, SCREEN_WIDTH, 200)];
        self.pickerView.delegate = self;
        self.pickerView.dataSource = self;
        self.pickerView.backgroundColor = [UIColor whiteColor];
        return self.pickerView;
        
    }
    
    return _inputView;
    
}

- (void)layoutSubviews{
    
    [super layoutSubviews];

}

#pragma mark - --------------------------UIPickerViewDataSource--------------------------
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return self.DATAArray.count;
    
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    return ((NSArray *)((self.DATAArray[component]))).count;
    
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component __TVOS_PROHIBITED{
    
    return 60;
}
#pragma mark - --------------------------UIPickerViewDelegate--------------------
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    return self.DATAArray[component][row];
    
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    UILabel *rowLabel = [[UILabel alloc]init];
    rowLabel.textAlignment = NSTextAlignmentCenter;
    if (self.detailArray.count != self.DATAArray.count) {
        
        rowLabel.text = [NSString stringWithFormat:@"%@",self.DATAArray[component][row]];
    }else
        
        rowLabel.text = [NSString stringWithFormat:@"%@%@",self.DATAArray[component][row],self.detailArray[component]];
    
    return rowLabel;
    
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (self.detailArray.count != 0) {
         return [[NSString stringWithFormat:@"%@%@",[self.DATAArray[component] lastObject],self.detailArray[component]]sizeWithFont:[UIFont systemFontOfSize:15]].width+20;//防止选中的时候会偏移
    }
    return [[NSString stringWithFormat:@"%@",[self.DATAArray[component] lastObject]]sizeWithFont:[UIFont systemFontOfSize:15]].width+20;//防止选中的时候会偏移
}

//选择pickerView显示内容的时候，需要重写这个方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    NSLog(@"选择中的内容 %@",self.DATAArray[component][row]);
    
}

- (void)awakeFromNib {
    
    [super awakeFromNib];

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    
    [super setSelected:selected animated:animated];

}

@end
